import { _decorator, Tween, tween, UIOpacity, UITransform } from 'cc';
import ViewAnimBase from './ViewAnimBase';
const { ccclass, property,menu } = _decorator;

@ccclass('ViewAnimFade')
@menu('ViewAnim/ViewAnimFade')
export default class ViewAnimFade extends ViewAnimBase {
    protected opacity: UIOpacity = null;
    protected _originOpacity: number = null;

    initialize(): void {
        this.opacity = this.node.getComponent( UIOpacity ) || this.node.addComponent( UIOpacity );
        this._originOpacity = this.opacity.opacity;
    }



    private tween: Tween<UIOpacity>;

    runInAction( duration: number): void {
        if( this.tween ) this.tween.stop();
        this.node.active = true;

        this.opacity.opacity = 0;
        this.tween = tween( this.opacity)
        .to( duration, {opacity: this._originOpacity})
        .start();
    }



    runOutAction( duration: number ): void {
        if( this.tween ) this.tween.stop();
        this.tween = tween( this.opacity ).to( duration, { opacity: 0}).call( () => this.node.active = false ).start();
    }
}


